package src.info
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	
	/**
	 * 拼字机配置表
	 * @author Anjue
	 */
	public class LetterInfo
	{
		public static var ownEvent:EventDispatcher = new EventDispatcher();
		
		private static var hash:Dictionary;
		
		/**
		 * 开始加载
		 */
		public static function doLoad():void
		{
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE,loadCom);
			loader.addEventListener(IOErrorEvent.IO_ERROR,loadError);
			loader.load(new URLRequest("resource/config/letterConfig.xml"));
		}
		
		/**
		 * 加载等级配置文件完成
		 * @param e
		 */
		private static function loadCom(e:Event):void
		{
			trace(e);
			var loader:URLLoader = e.currentTarget as URLLoader;
			loader.removeEventListener(Event.COMPLETE,loadCom);
			loader.removeEventListener(IOErrorEvent.IO_ERROR,loadError);
			
			var data:XML = XML(loader.data);
			hash=new Dictionary();
			for each (var item:XML in data.children())
			{
				var obj:Object={};
				obj.id=String(item.@id);
				obj.checkID=String(item.@checkID);
				obj.pin=String(item.@pin);
				obj.example=String(item.@example);
				hash[obj.id] = obj;
			}
			
			ownEvent.dispatchEvent(new Event(Event.COMPLETE));
		}
		
		/**
		 * 等级配置文件加载失败
		 * @param e
		 */
		private static function loadError(e:IOErrorEvent):void
		{
			var loader:URLLoader = e.currentTarget as URLLoader;
			loader.removeEventListener(Event.COMPLETE,loadCom);
			loader.removeEventListener(IOErrorEvent.IO_ERROR,loadError);
			
			trace(e);
		}
		
		/**
		 * 通过等级ID获取等级信息
		 * @param id
		 * @return 
		 */
		public static function getInfoByLevelID(id:String):Object
		{
			return hash[id];
		}
	}
}